//
//  CircleView-Album.m
//  16-CollectionView-Album
//
//  Created by 王道林 on 16/8/4.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "CircleView-Album.h"
#import "AlbumCollectionViewCell.h"
@interface CircleView_Album ()<UICollectionViewDelegate, UICollectionViewDataSource>


@property (nonatomic, strong) UICollectionView *collectionView;

@end



@implementation CircleView_Album

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createData];
        [self createSubView];
    }
    return self;
}

- (void)createData
{
    _marrPicture = [NSMutableArray arrayWithObjects:@"6", @"1", @"2", @"3", @"4", @"5", @"6", @"1", nil];
}



- (void)createSubView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = self.bounds.size;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;

    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    [self addSubview:_collectionView];
    
    [_collectionView registerClass:[AlbumCollectionViewCell class] forCellWithReuseIdentifier:@"wang"];
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _marrPicture.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wang" forIndexPath:indexPath];
    cell.iamgeView.image = [UIImage imageNamed:_marrPicture[indexPath.row]];
    return cell;
}


// 实现轮播
//轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.collectionView.contentOffset.x / self.bounds.size.width > self.marrPicture.count - 2)
    {
        self.collectionView.contentOffset = CGPointMake(self.bounds.size.width, 0);
   //     self.page.currentPage = 0;
    }
    else if(self.collectionView.contentOffset.x / self.bounds.size.width < 1)
    {
        self.collectionView.contentOffset = CGPointMake(self.bounds.size.width * (self.marrPicture.count - 2), 0);
 //       self.page.currentPage = self.marrPicture.count - 3;
    }else
    {
//        self.page.currentPage = (self.collection.contentOffset.x - self.bounds.size.width) / self.bounds.size.width;
    }
}



- (void)layoutSubviews
{
    
}








@end
